#
# photo_view.py - code to handle viewing of a photo stream from albums etc
#
# Copyright 2008-2009 Autelic Association (http://www.autelic.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi, sys, os, logging, string
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template
import wsgiref.handlers

import loggedin.profile
import pages

import atom.url
import gdata.service
import gdata.alt.appengine
import gdata.photos.service
import urllib # Used to unescape URL parameters.


################################# photo_view class ###########################
class photo_view(webapp.RequestHandler):
  def photoFeed(self, profile):
    # initialize a client to talk to Google Data API services
    client = gdata.photos.service.PhotosService()
    gdata.alt.appengine.run_on_appengine(client)  # run on App Engine
    html = ''

    try: # Attempt to fetch the feed of users own albums
      album_list = client.GetUserFeed(user=users.get_current_user().email())
      for album in album_list.entry:
         html = html + ('Own Album: <a href="%s">%s</a>, %s photos' % 
                (album.GetPhotosUri(), album.title.text.decode('UTF-8'), 
                 album.numphotos.text))
                 
         photos = client.GetFeed(album.GetPhotosUri()).entry
         if photos:
            html = html + '<ul>'
            for photo in photos:
               # TODO use the caption not the filename
               html = html + ('<li>%s</li>' % 
                              (photo.title.text.decode('UTF-8')))
            html = html + '</ul>'
    except gdata.service.RequestError, request_error:
      # TODO handle exceptions correctly
      html = "Error"
      # TODO for example, just because an autelic or google account exists that doesn't
      # mean a picasaweb user exists => "no such user' error from abovelbue
      
    # now load albums shared from other users - Picasaweb only at the moment
    # example Picasa URL: http://picasaweb.google.com/lh/sredir?uname=andrew.autelic.org&target=ALBUM&id=5292939029705623713&authkey=0LSF27v1cVk&feat=email
    for album in profile.shared_albums:
       if album.full_url:
          html = html + ('<a href="%s">Shared Album</a>' %
                         album.full_url)
       elif album.source == 'Picasa':
          html = html + ('Shared Album: <a href="http://picasaweb.google.com/lh/sredir?uname=%s&target=ALBUM&id=%s&authkey=%s&feat=email">%s</a><br>' % (album.user_id, album.album_id, album.auth_key, album.album_name))
          
    return html   

##############################################################################
##### get method for photo_view class
  def get(self):
    page = pages.Page('Photo Viewer')

    user_key = self.request.get('user_key')

    profile = db.get(user_key)

    # TODO TEMP - dump list of albums I have using following feed url
    page.add_html(self.photoFeed(profile))

    self.response.out.write(page.show())

## End of 'photo_view'
############################################

####### Main
def main(argv):
  application = webapp.WSGIApplication(
    [('/in/photo_view', photo_view)],
    debug=True)
    
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main(sys.argv)
